<?php

defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 订单的处理相关接口
 * 2017年6月9日10:36:59
 * @author yangwu
 */

class Order extends Web_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Order_model','order_model');
    	$this->load->model('Orderdetail_model');
	}

		
	/**
     * [updateOrderStatusById 修改订单详情套装码+修改订单状态]
     * @return [json] [10001=参数异常 10002=订单不存在或者已绑定 0=成功]
     */
    public function updateOrderStatusById()
    {
        $suitecode = $this->input->get('suitecode');//套装码
        $orderDetailid = $this->input->get('orderdetailid');
        $data = array('result'=>0,'errmsg'=>'ok');
        if(!ctype_digit($orderDetailid) || $suitecode =='' || $orderDetailid=='')
        {
            $data=array('result'=>10001,'errmsg'=>'参数异常');
            $this->response($data);
        }
        $number = $this->Orderdetail_model->updateOrderDetailBySuiteCode($orderDetailid, $suitecode);
        if($number === null)
        {
            $data = array('result'=>10002 ,'errmsg'=>'此订单不存在或者已绑定套装码');
            $this->response($data);
        }
        $order_id = $this->Orderdetail_model->getOrderInfoById($orderDetailid);
        if($order_id)
        {
            //处理完后解锁
            $params = array('is_lock'=>1,'update_time'=>time());
            $this->order_model->updateOrderByOrderid($order_id,$params);
        }
        $this->response($data);
    }
    /**
     * [updateOrderNumber 修改物流单号]
     * @return [type] [description]
     */
    public function updateOrderNumber()
    {
        $number = $this->input->get('number');//物流单号
        $order_id = $this->input->get('orderid');//订单编号
        $express_name = $this->input->get('express');//快递公司名
        $data = array('result'=>0,'errmsg'=>'ok');
        if(!ctype_digit($order_id) || empty($order_id) || empty($number) || empty($express_name))
        {
            $data = array('result'=>'10001','errmsg'=>'参数异常');
            $this->response($data);
        }
        $status = $this->order_model->updateLogistics($order_id,$number,$express_name);
        if( is_null($status) )
        {
            $data = array('result'=>'10002','errmsg'=>'订单不存在');
            $this->response($data);
        }
        if($status === false)
        {
            $data = array('result'=>'10003','errmsg'=>'该单号已存在物流记录');
            $this->response($data);
        }
        $this->response($data);
    }

    /**
     * 根据订单号查询订单是否超时(查询redis缓存)
     */
    public function CheckOrderTimeOut()
    {
        $order_id = $this->input->post('order_id');
        if(! ctype_digit($order_id))
        {
            echo json_encode(array('errcode' => 2, 'errmsg' => '参数错误'));
            return;
        }

        $this->load->library('RedisDriver', 'redisdriver');
        $this->redisdriver->connect();
        $orderData = $this->redisdriver->getStr('order_'.$order_id);
        if(empty($orderData))
        {
            echo json_encode(array('errcode' => 0, 'errmsg' => '订单超时'));
        }
        else
        {
            echo json_encode(array('errcode' => 1, 'errmsg' => '订单未超时'));
        }
    }

    /**
     * [getOrderDetail 获取一条订单记录或者根据订单id查询一条记录]
     * @return [type] [description]
     */
    public function getOrderDetail()
    {
        $fp = fopen("license.txt", "r+");
        if (flock($fp, LOCK_EX)) {

            $orderid = $this->input->get('orderid');
            $type = $this->input->get('type');
            $msg = array('result'=>0,'errmsg'=>'ok');
            if(isset($orderid) && !ctype_digit($orderid))
            {
                $msg = array('result'=>'10001','errmsg'=>'参数异常');
                $this->response($msg);
            }
            if(isset($type) && $type != 1)
            {
                $msg = array('result'=>'10001','errmsg'=>'参数异常');
                $this->response($msg);
            }
         
            $data = $this->order_model->getRowOrderData($orderid,$type);
            if(empty($data))
            {
                $msg = array('result'=>'10002','errmsg'=> isset($orderid) ? '该订单不存在':'暂时没有需要处理的订单');
                $this->response($msg);
            }
            if(isset($data['lock']))
            {
                $msg['is_lock'] = $data['lock'];//被锁订单
                $msg['count'] = $data['count'];//总数量
                $msg['is_ok'] = $data['dispose'];//处理完成的
                $msg['is_no'] = $data['undone'];//未处理完成的

                unset($data['lock'],$data['count'],$data['dispose'],$data['undone']);
            }

            if( count($data) == count($data, 1)){

                $data = empty($data) ? $data:array($data);
            }
            if(isset($type))
            {   
                if(count($data) == 0)
                {
                    unset($data[0]);
                }   
               
            }
            $msg['data'] =  $data;
            flock($fp, LOCK_UN);  
            fclose($fp);
            $this->response($msg);
        } 
    }
}